package ua.mykola.challange.chapter1;

public class ThreeNPlusOne
{
    public int longestSequenceLength(int i, int j)
    {
        int longest = 0;
        int min, max;
        if (i < j)
        {
            min = i;
            max = j;
        }
        else
        {
            min = j;
            max = i;
        }
        while (min <= max)
        {
            int sequenceLength = sequenceLength(min++);
            if (sequenceLength > longest)
            {
                longest = sequenceLength;
            }
        }
        return longest;
    }

    private int sequenceLength(int i)
    {
        int length = 1;
        long result = i;

        while (result != 1)
        {
            length++;
            if (result % 2 == 0)
            {
                result >>= 1;
            }
            else
            {
                result = 3 * result + 1;
            }
        }
        return length;
    }

}
